package pojo;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class GoogleWeatherParser {
	public static WeatherForecast parser(String city) {
		WeatherForecast wf = new WeatherForecast();
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
				.newInstance();

		InputStream byteStream;
		DocumentBuilder docBuilder;
		try {
			byteStream = new URL("http://www.google.com/ig/api?weather=" + city
					+ "&hl=Zh-CN").openStream();

			InputSource is = new InputSource();
			is.setEncoding("GBK");
			is.setByteStream(byteStream);

			docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(is);

			// if return data, length is 6
			// 1st, city info
			// 2nd, current condition
			// 3rd, today
			// 4th, tomorrow
			// 5th, the day after tomorrow
			// 6th, in three days

			NodeList nl = doc.getFirstChild().getFirstChild().getChildNodes();
			// nl has all days' condition
			// start with 1 because ignore forecast_information
			for (int i = 1; i < nl.getLength(); i++) {
				if (i == 1) {
					wf.setCurrent(weatherProducer(nl.item(i)));
				} else if (i == 2) {
					wf.setToday(weatherProducer(nl.item(i)));
				} else if (i == 3) {
					wf.setTomorrow(weatherProducer(nl.item(i)));
				} else if (i == 4) {
					wf.setThedayAfterTomorrow(weatherProducer(nl.item(i)));
				} else if (i == 5) {
					wf.setInThreeDays(weatherProducer(nl.item(i)));
				}
			}
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		} catch (SAXException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return wf;
	}

	public static Weather weatherProducer(Node node) {
		Weather w = new Weather();
		NodeList nodeList = node.getChildNodes();
		// get all leaf nodes, such as low or high or condition
		int length = nodeList.getLength();
		while (length > 0) {
			Node leaf = nodeList.item(length - 1);
			NamedNodeMap nnm = leaf.getAttributes();
			if ("condition".equalsIgnoreCase(leaf.getNodeName())) {
				w.setCondition(nnm.item(0).getNodeValue());
			} else if ("condition".equalsIgnoreCase(leaf.getNodeName())) {
				w.setCondition(nnm.item(0).getNodeValue());
			} else if ("low".equalsIgnoreCase(leaf.getNodeName())) {
				w.setLow(nnm.item(0).getNodeValue());
			} else if ("high".equalsIgnoreCase(leaf.getNodeName())) {
				w.setHigh(nnm.item(0).getNodeValue());
			} else if ("temp_c".equalsIgnoreCase(leaf.getNodeName())) {
				w.setCurrentTemp(nnm.item(0).getNodeValue());
			} else if ("humidity".equalsIgnoreCase(leaf.getNodeName())) {
				w.setHumidity(nnm.item(0).getNodeValue());
			} else if ("wind_condition".equalsIgnoreCase(leaf.getNodeName())) {
				w.setWindCondition(nnm.item(0).getNodeValue());
			} else if ("icon".equalsIgnoreCase(leaf.getNodeName())) {
				w.setIcon("http://www.google.com" + nnm.item(0).getNodeValue());
			} else if ("day_of_week".equalsIgnoreCase(leaf.getNodeName())) {
				w.setDayOfWeek(nnm.item(0).getNodeValue());
			}
			length--;
		}

		return w;
	}
}
